//
//  StepViewController.m
//  YunuoHealth
//
//  Created by 孙博 on 15/9/15.
//
//

#import "StepViewController.h"
#import "ToolHelper.h"
#define SCREEN_FRAME ([UIScreen mainScreen].bounds)  
@interface StepViewController ()
{
    UIPageControl *pageControl; //指示当前处于第几个引导页
    UIScrollView *scrollView; //用于存放并显示引导页
    UIImageView *imageViewOne;
    UIImageView *imageViewTwo;
    UIImageView *imageViewThree;
    UIImageView * logoImage;
}
@end

@implementation StepViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.
    
    //初始化UI控件
    scrollView=[[UIScrollView alloc]initWithFrame:SCREEN_FRAME];
    scrollView.pagingEnabled=YES;
    [self.view addSubview:scrollView];
    
    pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT * 0.94, SCREEN_WIDTH, 10)];
    pageControl.pageIndicatorTintColor=SystemColor;
    pageControl.currentPageIndicatorTintColor=[UIColor colorWithRed:0.153 green:0.533 blue:0.796 alpha:1.0];
    [self.view addSubview:pageControl];
    pageControl.numberOfPages=3;
    
    [self createViewOne];
    [self createViewTwo];
    [self createViewThree];
    
    
}

-(void)addLogoImage:(UIView*)view
{
    logoImage=[[UIImageView alloc]init];
    logoImage.translatesAutoresizingMaskIntoConstraints=NO;
    logoImage.image=[UIImage imageNamed:@"logo"];
    [view addSubview:logoImage];
    [view addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(logoImage) format:@"H:[logoImage(80)]-20-|"]];
    [view addConstraints:[ToolHelper GetNSLayoutCont:NSDictionaryOfVariableBindings(logoImage) format:@"V:|-10-[logoImage(50)]"]];
}
-(void)createViewOne{
    
    UIView *view = [[UIView alloc] initWithFrame:SCREEN_FRAME];
    
    imageViewOne = [[UIImageView alloc] initWithFrame:SCREEN_FRAME];
    imageViewOne.contentMode = UIViewContentModeScaleAspectFit;
    if ([ToolHelper IsChineseLanguage]) {
        imageViewOne.image = [UIImage imageNamed:@"step1"];
    }
    else
    {
       imageViewOne.image = [UIImage imageNamed:@"e_step1"];
    }
    [view addSubview:imageViewOne];
    [self addLogoImage:view];
    
    UISwipeGestureRecognizer *singleTap1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress1:)];
    imageViewOne.userInteractionEnabled = YES;
    [imageViewOne addGestureRecognizer:singleTap1];
    singleTap1.direction = UISwipeGestureRecognizerDirectionLeft;
    
    [scrollView addSubview:view];
    
}

-(void)createViewTwo{
    
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    imageViewTwo = [[UIImageView alloc] initWithFrame:SCREEN_FRAME];
    imageViewTwo.contentMode = UIViewContentModeScaleAspectFit;
    if ([ToolHelper IsChineseLanguage]) {
        imageViewTwo.image = [UIImage imageNamed:@"step2"];
    }
    else
    {
        imageViewTwo.image = [UIImage imageNamed:@"e_step2"];
    }
    [view addSubview:imageViewTwo];
    [self addLogoImage:view];
    UISwipeGestureRecognizer *singleTap1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress2:)];
    imageViewTwo.userInteractionEnabled = YES;
    [imageViewTwo addGestureRecognizer:singleTap1];
    singleTap1.direction = UISwipeGestureRecognizerDirectionLeft;
    
    singleTap1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress2:)];
    imageViewTwo.userInteractionEnabled = YES;
    [imageViewTwo addGestureRecognizer:singleTap1];
    singleTap1.direction = UISwipeGestureRecognizerDirectionRight;
    
    [scrollView addSubview:view];
    
}

-(void)createViewThree{
    
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*2, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    
    imageViewThree = [[UIImageView alloc] initWithFrame:SCREEN_FRAME];
    imageViewThree.contentMode = UIViewContentModeScaleAspectFit;
    if ([ToolHelper IsChineseLanguage]) {
        imageViewThree.image = [UIImage imageNamed:@"step3"];
    }
    else
    {
        imageViewThree.image = [UIImage imageNamed:@"e_step3"];
    }
    [view addSubview:imageViewThree];
    [self addLogoImage:view];
    UISwipeGestureRecognizer  * singleTap1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress3:)];
    imageViewThree.userInteractionEnabled = YES;
    [imageViewThree addGestureRecognizer:singleTap1];
    singleTap1.direction = UISwipeGestureRecognizerDirectionRight;
    
    UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress4:)];
    imageViewThree.userInteractionEnabled = YES;
    [imageViewThree addGestureRecognizer:singleTap];
    
    [scrollView addSubview:view];
}

#pragma mark -- tap image
-(void)buttonpress1:(UISwipeGestureRecognizer*)sender
{
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
    CGFloat pageWidth = CGRectGetWidth(self.view.bounds);
    CGPoint scrollPoint = CGPointMake(pageWidth, 0);
    [scrollView setContentOffset:scrollPoint animated:YES];
    
    pageControl.currentPage = 1;
    }
}

-(void)buttonpress2:(UISwipeGestureRecognizer*)sender
{
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
    {
    CGFloat pageWidth = CGRectGetWidth(self.view.bounds);
    CGPoint scrollPoint = CGPointMake(pageWidth*2, 0);
    [scrollView setContentOffset:scrollPoint animated:YES];
    pageControl.currentPage = 2;
    }
    else
    {
        CGFloat pageWidth = CGRectGetWidth(self.view.bounds);
        CGPoint scrollPoint = CGPointMake(0, 0);
        [scrollView setContentOffset:scrollPoint animated:YES];
        
        pageControl.currentPage = 0;
    }
    
}

-(void)buttonpress3:(id)sender
{
    CGFloat pageWidth = CGRectGetWidth(self.view.bounds);
    CGPoint scrollPoint = CGPointMake(pageWidth, 0);
    [scrollView setContentOffset:scrollPoint animated:YES];
    pageControl.currentPage = 1;
    
}


-(void)buttonpress4:(id) sender
{
    if(self._SetpViewEndBlock)
    {
        self._SetpViewEndBlock();
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
